﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AllenData
{
    public class InteractionTypeData
    {
        public InteractionType GetInteractionType(int interactionTypeId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                var interactionType = ctx.InteractionTypes.Where(d => d.InteractionTypeID == interactionTypeId);
                return interactionType.FirstOrDefault();
            }
        }

        public List<InteractionType> GetAllInteractionTypes()
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                return ctx.InteractionTypes.ToList();
            }
        }

        public int AddNewInteractionType(InteractionType interactionType)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                ctx.AddToInteractionTypes(interactionType);
                ctx.SaveChanges();

                InteractionType newInteractionType = ctx.InteractionTypes.Where(
                    d => d.Name == interactionType.Name).FirstOrDefault();

                if (newInteractionType == null)
                    return -1;
                else
                    return newInteractionType.InteractionTypeID;
            }
        }

        public void UpdateInteractionType(InteractionType interactionType)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                InteractionType dbDept = ctx.InteractionTypes.First(d => d.InteractionTypeID == interactionType.InteractionTypeID);
                ctx.ApplyCurrentValues("InteractionTypes", interactionType);
                ctx.SaveChanges();
            }
        }

        public void DeleteInteractionType(int interactionTypeId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                InteractionType dbDept = ctx.InteractionTypes.First(d => d.InteractionTypeID == interactionTypeId);
                ctx.DeleteObject(dbDept);
                ctx.SaveChanges();
            }
        }
    }
}
